package com.wlf.constant;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author lingfeng.wei
 * @date 2022/1/12 10:01
 */
@Component
public class Fonts {

    @Value("${env.file.temp.path}")
    private String path;

    //项目第一次启动默认更新字体
    public static Boolean IS_COPY_FOP_FILE =false;

    public final static String ARIAL_TTF="arial.ttf";
    public final static String ARIAL_XML="arial.xml";

    public final static String FZYTK_TTF="FZYTK.TTF";
    public final static String FZYTK_XML="FZYTK.xml";

    public final static String MSYH_TTF="msyh.ttf";
    public final static String MSYH_XML="msyh.xml";

    public final static String MSYHBD_TTF="msyhbd.ttf";
    public final static String MSYHBD_XML="msyhbd.xml";

    public final static String SEGUISYM_TTF="seguisym.ttf";
    public final static String SEGUISYM_XML="seguisym.xml";

    public final static String SIMHEI_TTF="simhei.ttf";
    public final static String SIMHEI_XML="simhei.xml";

    public final static String SIMSUN_TTC="SIMSUN.TTC";
    public final static String SIMSUN_XML="simsun.xml";

    public final static String SONGHEI_TTF="songhei.ttf";
    public final static String SONGHEI_XML="songhei.xml";

    public final static String TAHOMA_TTF="tahoma.ttf";
    public final static String TAHOMA_XML="tahoma.xml";

    public final static String TAHOMABD_TTF="tahomabd.ttf";
    public final static String TAHOMABD_XML="tahomabd.xml";

    public final static String[] FOP_FONTS=new String[]{ARIAL_TTF,ARIAL_XML,FZYTK_TTF,FZYTK_XML,MSYH_TTF
            ,MSYH_XML,MSYHBD_TTF,MSYHBD_XML,SEGUISYM_TTF,SEGUISYM_XML,SIMHEI_TTF,SIMHEI_XML,SIMSUN_TTC,
            SIMSUN_XML,SONGHEI_TTF,SONGHEI_XML,TAHOMA_TTF,TAHOMA_XML,TAHOMABD_TTF,TAHOMABD_TTF};

    public final static String FOP_URL="fop.xml";

    //字体所在目录
    public final static String FOP="fop";

    /**
     * 获取字体跟文件
     * 正常情况下只复制一次文件，除非文件删除则需要将 resoruce下的所有文件复制到系统文件目录
     * @return
     */
    private String getFopUrl(){
        try{
            //项目每次次启动后访问更新下载字体
            if(!Fonts.IS_COPY_FOP_FILE){
                File dir =new File(path);
                //创建字体存放目录
                if(!dir.exists()){
                    //创建目录
                    dir.mkdirs();
                }
                //复制字体到该目录下
                for (String fileName : Fonts.FOP_FONTS) {
                    File file = new File(path + File.separator+ fileName);
                    if (file.exists()){
                        file.delete();
                    }
                    file.createNewFile(); //创建文件
                    OutputStream out = new FileOutputStream(file);
                    Resource resource = new ClassPathResource(Fonts.FOP+File.separator+fileName);
                    InputStream is = resource.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    byte[] buf = new byte[2048];
                    int len = 0;
                    while ((len = bis.read(buf)) != -1)
                        out.write(buf, 0, len);
                    bis.close();
                    out.flush();
                    is.close();
                    if (out != null) {
                        out.close();
                    }
                }
            }

            //检查FOP文件
            File file = new File(path + File.separator +Fonts.FOP_URL);
            if(!Fonts.IS_COPY_FOP_FILE){
                if(file.exists()){
                    file.delete();
                }
                file.createNewFile(); //创建文件
                OutputStream out = new FileOutputStream(file);
                Resource resource = new ClassPathResource(Fonts.FOP+File.separator+Fonts.FOP_URL);
                InputStream is = resource.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                byte[] buf = new byte[2048];
                int len = 0;
                while ((len = bis.read(buf)) != -1)
                    out.write(buf, 0, len);
                bis.close();
                out.flush();
                is.close();
                if (out != null) {
                    out.close();
                }
                Fonts.IS_COPY_FOP_FILE=Boolean.TRUE;
            }
            return file.getPath();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
